package com.nian.mongodb.service.service.impl;

import com.nian.mongodb.service.dao.CompanyRepository;
import com.nian.mongodb.service.model.Company;
import com.nian.mongodb.service.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/12 0012 18:38
 */
@Service
public class CompanyServiceImpl implements ICompanyService {

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Company findByCode(String code) {
        if(StringUtils.isEmpty(code)){
            return null;
        }
        return companyRepository.findByCode(code);
    }

    @Override
    public List<Company> findByLikeName(String name) {
        if(StringUtils.isEmpty(name)){
            return null;
        }
        ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        Company company = new Company();
        company.setName(name);
        Example<Company> example = Example.of(company, matcher);
        return companyRepository.findAll(example);
    }

    @Override
    public List<Company> findByName(String name) {
        if(StringUtils.isEmpty(name)){
            return null;
        }
        return companyRepository.findByName(name);
    }

    @Override
    public Company save(Company model) {
        return companyRepository.save(model);
    }

    @Override
    public void saveAll(List<Company> list) {
        companyRepository.saveAll(list);
    }
}
